{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#pip install opencv-python\n",
    "#pip install opencv-python-headless  // gl error\n",
    "#pip install opencv-contrib-python\n",
    "\n",
    "# cv2.imshow('image',img)\n",
    "# cv2.waitKey(0)\n",
    "# cv2.destroyAllWindows()\n",
    "# cv2.waitKey(1) # 部分设备会卡住"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 简单绘图API调用 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "img = np.full((600,800,3),255,np.uint8)  # \n",
    "# print(img.shape)\n",
    "\n",
    "# 画线 pt1:起点 pt2:终点 color:颜色 (255,0,0) thickness: 线宽  （非线性图型-1:填充） lineType:线型 抗锯齿  shift:缩小到 1 / （2**shift）\n",
    "cv2.line(img,pt1=(0,0),pt2=(800,400),color=(0,255,0), thickness=10) \n",
    "\n",
    "# 画圆 center:圆心 radius:半径 color:颜色 \n",
    "cv2.circle(img,center=(400,300), radius=100, color=(255,0,0), thickness=1,lineType=cv2.LINE_AA)  \n",
    "cv2.circle(img,center=(400,300), radius=120, color=(255,255,0), thickness=1,lineType=cv2.LINE_8) \n",
    "cv2.circle(img,center=(400,300), radius=140, color=(255,0,255), thickness=1,lineType=cv2.LINE_4)\n",
    "cv2.circle(img,center=(400,300), radius=170, color=(0,0,255), thickness=1) \n",
    "\n",
    "# 画矩形 pt1:左上角 pt2:右下角\n",
    "cv2.rectangle(img,pt1=(100,100),pt2=(300,300),color=(0,0,255), thickness= 2)\n",
    "cv2.rectangle(img,pt1=(100,100),pt2=(300,300),color=(0,255,255), thickness= 2,shift=1) \n",
    "\n",
    "\n",
    "# 画椭圆 center:圆心 axes:长短轴 angle:旋转角度 startAngle:起始角度 endAngle:终止角度\n",
    "cv2.ellipse(img,center=(400,300), axes=(100,50), angle=80, startAngle=90, endAngle=270, color=(0,0,255), thickness=2)\n",
    "\n",
    "\n",
    "# 画文字 text:文字 org:起始位置(文字左下角对齐) fontFace:字体 fontScale:字体大小 color:颜色 thickness:线宽\n",
    "cv2.putText(img,text='Hello',org=(100,100),fontFace=cv2.FONT_HERSHEY_SIMPLEX,fontScale=2,color=(0,0,255),thickness=2,lineType=cv2.LINE_AA)\n",
    "\n",
    "cv2.circle(img,center=(100,100), radius=1, color=(255,0,0), thickness=2,lineType=cv2.LINE_AA)  \n",
    "\n",
    "# plt.figure(figsize=(8,6))\n",
    "# img[:,:,0] = 0\n",
    "# print(img)\n",
    "\n",
    "plt.xticks([])\n",
    "plt.yticks([])\n",
    "_=plt.imshow(img)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 绘制 opencv 图标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import cv2 as cv\n",
    "import matplotlib.pyplot as plt\n",
    "import math\n",
    " \n",
    "H = 1000\n",
    "W = 1000 \n",
    " \n",
    "# 放置一个白色底的画布\n",
    "bgColor = (255,255,255)\n",
    "colorRed = (0,0,255)\n",
    "colorBlue = (255,0,0)\n",
    "colorGreen = (0,255,0)\n",
    "img = np.full((H, W, 3),bgColor[0], np.uint8)\n",
    " \n",
    "# 提前设定画布的长宽以及下方文字大概所占的长度\n",
    "D = int(0.2 * W)\n",
    " \n",
    "# 圆中心点\n",
    "circleGreenCenter = (int(0.5 * W), int(0.4 * W - D))\n",
    "circleRedCenter = (int(0.5 * W - math.sqrt(3)/2 * D), int(0.4 * W + 0.5 * D))\n",
    "circleBlueCenter = (int(0.5 * W + math.sqrt(3)/2 * D), int(0.4 * W + 0.5 * D))\n",
    "\n",
    "\n",
    "# 外圆的半径\n",
    "R1 = int(math.sqrt(3) * 0.5 * D)\n",
    " \n",
    "# 小圆的半径\n",
    "R2 = int(math.sqrt(3) * 0.3 * D)\n",
    " \n",
    "\n",
    "\n",
    "thickness = R1 - R2 \n",
    "largeRadius = R1 - thickness\n",
    "# 绘制大圆环\n",
    "cv.circle(img, circleGreenCenter, largeRadius, colorGreen, thickness,lineType=cv.LINE_AA)\n",
    "cv.circle(img, circleRedCenter, largeRadius, colorRed, thickness,lineType=cv.LINE_AA)\n",
    "cv.circle(img, circleBlueCenter, largeRadius, colorBlue, thickness,lineType=cv.LINE_AA)\n",
    "\n",
    "# 旋转的角度\n",
    "ang = 60\n",
    "ellipsisThickness = -1\n",
    "# 使用椭圆绘制扇形形\n",
    "cv.ellipse(img, circleGreenCenter, (R1,R1), ang, 0, ang, bgColor, ellipsisThickness)\n",
    "cv.ellipse(img, circleRedCenter, (R1,R1), 360 - ang, 0, ang, bgColor, ellipsisThickness)\n",
    "cv.ellipse(img, circleBlueCenter, (R1,R1), 360 - 2 * ang, 0, ang, bgColor, ellipsisThickness)\n",
    " \n",
    "# 绘制文本\n",
    "font = cv.FONT_HERSHEY_SIMPLEX\n",
    "fontSize = W * 0.007\n",
    "fontThickness = int(W * 0.02)\n",
    "title = 'OpenCV'\n",
    "titleColor = (0,0,0)\n",
    "size, baseline = cv.getTextSize(title, font, fontSize, fontThickness)\n",
    "cv.putText(img, title, (int((W-size[0])/2) ,H - D + baseline), font, fontSize, titleColor, fontThickness,lineType=cv.LINE_AA)\n",
    "\n",
    "plt.xticks([])\n",
    "plt.yticks([]) \n",
    "plt.imshow(img)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
